﻿using System;
using System.Collections.Concurrent;

namespace System.Erp
{
    /// <summary>
    /// 标记在服务接口或类中，表示这个服务是使用TypeKey分类处理的。 
    /// </summary>
    [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
    public sealed class TypeKeyOnlyAttribute : Attribute
    {
        private static readonly ConcurrentDictionary<Type, bool> _typeKeyServiceCache = new ConcurrentDictionary<Type, bool>();
        private static readonly Type TYPE_KEY_ONLY_ATTRIBUTE_TYPE = typeof(TypeKeyOnlyAttribute);

        /// <summary>
        /// 返回指定的服务是否是使用TypeKey分类处理
        /// </summary>
        /// <param name="serviceType">要检测的服务类型</param>
        /// <returns>如果是返回true，否则返回false</returns>
        public static bool IsTypeKeyOnly(Type serviceType)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException("serviceType");
            }

            return _typeKeyServiceCache.GetOrAdd(serviceType, (t) => t.IsDefined(TYPE_KEY_ONLY_ATTRIBUTE_TYPE,false));
        }

    }
}
